<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Accessing Deeply Nested Elements</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../dom.html" title="Document Model">
<link rel="prev" href="initializer_lists.html" title="Initializer Lists">
<link rel="next" href="../allocators.html" title="Allocators">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="initializer_lists.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../allocators.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.dom.nested_access"></a><a class="link" href="nested_access.html" title="Accessing Deeply Nested Elements">Accessing Deeply Nested Elements</a>
</h3></div></div></div>
<p>
        In order to allow conveniently retrieving and changing deeply nested elements
        of <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>
        objects the library implements <a href="https://datatracker.ietf.org/doc/html/rfc6901" target="_top">RFC
        6901 (JSON Pointer)</a>:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span><span class="string">"one"</span><span class="special">,</span> <span class="number">1</span><span class="special">},</span> <span class="special">{</span><span class="string">"two"</span><span class="special">,</span> <span class="number">2</span><span class="special">}</span> <span class="special">};</span>
<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"one"</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at_pointer</span><span class="special">(</span><span class="string">"/one"</span><span class="special">)</span> <span class="special">);</span>

<span class="identifier">jv</span><span class="special">.</span><span class="identifier">at_pointer</span><span class="special">(</span><span class="string">"/one"</span><span class="special">)</span> <span class="special">=</span> <span class="special">{{</span><span class="string">"foo"</span><span class="special">,</span> <span class="string">"bar"</span><span class="special">}};</span>
<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"one"</span><span class="special">).</span><span class="identifier">at</span><span class="special">(</span><span class="string">"foo"</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at_pointer</span><span class="special">(</span><span class="string">"/one/foo"</span><span class="special">)</span> <span class="special">);</span>

<span class="identifier">jv</span><span class="special">.</span><span class="identifier">at_pointer</span><span class="special">(</span><span class="string">"/one/foo"</span><span class="special">)</span> <span class="special">=</span> <span class="special">{</span><span class="keyword">true</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="string">"qwerty"</span><span class="special">};</span>
<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"one"</span><span class="special">).</span><span class="identifier">at</span><span class="special">(</span><span class="string">"foo"</span><span class="special">).</span><span class="identifier">at</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">at_pointer</span><span class="special">(</span><span class="string">"/one/foo/1"</span><span class="special">)</span> <span class="special">);</span>
</pre>
<p>
        This is particularly useful when throwing exceptions is undesirable. For
        example, compare
      </p>
<pre class="programlisting"><span class="identifier">object</span><span class="special">*</span> <span class="identifier">obj</span> <span class="special">=</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">if_object</span><span class="special">();</span>
<span class="keyword">if</span><span class="special">(</span> <span class="special">!</span><span class="identifier">obj</span> <span class="special">)</span>
    <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>

<span class="identifier">value</span><span class="special">*</span> <span class="identifier">val</span> <span class="special">=</span> <span class="identifier">obj</span><span class="special">-&gt;</span><span class="identifier">if_contains</span><span class="special">(</span><span class="string">"one"</span><span class="special">);</span>
<span class="keyword">if</span><span class="special">(</span> <span class="special">!</span><span class="identifier">val</span> <span class="special">)</span>
    <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>

<span class="identifier">obj</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">-&gt;</span><span class="identifier">if_object</span><span class="special">();</span>
<span class="keyword">if</span><span class="special">(</span> <span class="special">!</span><span class="identifier">obj</span> <span class="special">)</span>
    <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>

<span class="identifier">val</span> <span class="special">=</span> <span class="identifier">obj</span><span class="special">-&gt;</span><span class="identifier">if_contains</span><span class="special">(</span><span class="string">"foo"</span><span class="special">);</span>
<span class="keyword">if</span><span class="special">(</span> <span class="special">!</span><span class="identifier">val</span> <span class="special">)</span>
    <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>

<span class="identifier">array</span><span class="special">*</span> <span class="identifier">arr</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">-&gt;</span><span class="identifier">if_array</span><span class="special">();</span>
<span class="keyword">if</span><span class="special">(</span> <span class="special">!</span><span class="identifier">arr</span> <span class="special">)</span>
    <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>

<span class="keyword">return</span> <span class="identifier">arr</span><span class="special">-&gt;</span><span class="identifier">if_contains</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
</pre>
<p>
        with
      </p>
<pre class="programlisting"><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
<span class="keyword">return</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">find_pointer</span><span class="special">(</span><span class="string">"/one/foo/1"</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="initializer_lists.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../allocators.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
